<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');


if (!function_exists('generate_input_element')) {
    
    /*
     * creates html input elements 
     */
    function generate_input_element($index, $methods='') {
        $ret = '';
        $rare_input = array('select', 'textarea');
        $input_name = '';
        $attributes = array();
        $type = 'text';
        $value = '';
        
            (isset($methods['value'])) ? $attributes['value'] = $value = $methods['value'] : $attributes['value'] = '';
            if (!is_array($methods)) {
                $attributes['type'] = $type;
                $attributes['name'] = $index;
                $attributes['id'] = $index;
                //$attributes['id'] = $v;
            } else {
                $attributes = $methods;
                
                $attributes['name'] = $index;
                $attributes['id'] = $index;
                //$attributes['id'] = $i;
                (isset($attributes['type'])) ? $type = $attributes['type'] : $attributes['type'] = $type;

                if (isset($attributes['type']) && in_array($type, $rare_input)) {
                     
                    unset($attributes['type']);
                    unset($attributes['value']);
                }
            }


        $str_attr = generate_attributes($attributes);

        switch ($type) {
            case 'select':
                $ret .= '<' . $type . $str_attr . '>' . generate_select_options($attributes['options'], $value) . '</' . $type . '>' . "\n";
                break;
            case 'textarea':
                $ret .= '<' . $type . $str_attr . '>' . $value . '</' . $type . '>' . "\n";
                break;
            default:
                $ret .= '<input' . $str_attr . ' />' . "\n";
                break;
        }
        return $ret;
    }

}

if(!function_exists('collect_inputs')){
    function collect_inputs(&$attributes){
        foreach ($attributes as $i => $v){
            $attributes[$i]['input'] = generate_input_element($i, $v);
        }
    }
}

if(!function_exists('init_values')){
    function init_values(&$attibutes, $values){
        foreach ($attibutes as $i => $v){
            $attibutes[$i]['value'] = $values[$i];
        }
    }
}

if (!function_exists('generate_select_options')) {

    function generate_select_options($param, $value) {
        $ret = '';
        if (is_array($param)) {
            foreach ($param as $i => $v) {
                $ret .= '<option value="' . $i . '"' . (($value == $i) ? 'selected="selected"' : '') . '>' . $v . '</option>' . "\n";
            }
        }
        return $ret;
    }
}


//erythrocin
/*
 * generate an html tag attribute with value on it
 */